package com.hnl.action.user;

import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hnl.core.HNLPageDispatch;
import com.hnl.core.aware.HNLAuthAware;
import com.hnl.dao.DAOException;
import com.hnl.dao.FunctionDAO;
import com.hnl.dao.HNLDAOFactory;
import com.hnl.dao.UserDAO;
import com.hnl.to.Function;
import com.hnl.to.User;
import com.hnl.to.wrapper.RoleFunctionWrapper;
import com.hnl.util.JSONResponseUtil;

@SuppressWarnings("serial")
public class UserLogin extends HttpServlet implements HNLAuthAware,
		HNLPageDispatch {
	public static final Logger LOG = LoggerFactory.getLogger(UserLogin.class);

	private final String LOGIN_ERROR = "Login failed. Please verify your username / password or contact the System Administrator";

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			String username = req.getParameter("username");
			String password = req.getParameter("password");

			RequestDispatcher dispatcher = req.getRequestDispatcher(LOGIN_PAGE);

			if (username != null) {
				UserDAO userDAO = HNLDAOFactory.getDAOFactory(
						HNLDAOFactory.HNLDAOFACTORY).getUserDAO();
				User user = userDAO.loadUserByUsername(username);
				
				if (user != null && user.getPassword() != null
						&& user.getPassword().equals(password)) {
					HttpSession session = req.getSession();
					session.setAttribute(ATTRIBUTE_USER, user);
					Date now = new Date();
					
					user.setLastLoginDate(now);					
					userDAO.updateUser(user);

					FunctionDAO roleFunctionDAO = HNLDAOFactory.getDAOFactory(
							HNLDAOFactory.HNLDAOFACTORY).getFunctionDAO();
					Collection<Function> functionCollection = roleFunctionDAO
							.loadFunctionsByRoleID(user.getRoleID());

					Collection<RoleFunctionWrapper> roleFunctionWrapperColletion = new Vector<RoleFunctionWrapper>();

					for (Function roleFunction : functionCollection) {
						RoleFunctionWrapper newRoleFunctionWrapper = new RoleFunctionWrapper(
								roleFunction);
						newRoleFunctionWrapper.getFunctionText();
						roleFunctionWrapperColletion
								.add(newRoleFunctionWrapper);
					}

					session.setAttribute(
							ATTRIBUTE_ROLE_FUNCTIONS_MAP,
							JSONResponseUtil
									.getJSONObjectString(roleFunctionWrapperColletion));

					dispatcher = req.getRequestDispatcher(DASHBOARD_PAGE);
				} else {
					req.setAttribute("error", LOGIN_ERROR);
				}
			}
			dispatcher.forward(req, resp);
		} catch (DAOException e) {
			LOG.error("Error logging in.", e);
		}
	}
}
